package gamecode;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Point2D;

public class Entity {

	protected final double maxShipTurnSpeed = 3;
	protected final double maxShipMoveSpeed = 1;
	private DPoint position = new DPoint();
	private double direction = 0;
	private Vector2D velocity = new Vector2D();
	private Vector2D acceleration = new Vector2D();
	private double directionAcceleration = 0;
	
	public DPoint getPosition() {
		return position;
	}
	
	public void setPosition(double x, double y) {
		position.setLocation(x, y);
	}
	
	public void setPosition(DPoint pos) {
		position.setLocation(pos);
	}
	
	public void setX(double x) {
		position.setLocation(x, position.getY());
	}
	
	public void setY(double y) {
		position.setLocation(position.getX(), y);
	}
	
	public void move(Point2D p) {
		position.translate(p.getX(), p.getY());
	}
	
	public void move(double x, double y) {
		position.translate(x, y);
	}
	
	public double getDirection() {
		return direction;
	}
	
	public void setDirection(double d) {
		direction = Vector2D.wrapDirection(d);
	}
	
	public void rotate(double d) {
		direction = Vector2D.wrapDirection(direction + d);
	}
	
	public void rotateAcceleration(double d) {
		directionAcceleration = Vector2D.wrapDirection(d);
	}
	
	public Vector2D getVelocity() {
		return velocity;
	}
	
	public void setVelocity(Vector2D vec) {
		velocity.setVector(vec);
	}
	
	public void setVelocity(double d, double m) {
		velocity.setDirection(d);
		velocity.setMagnitude(m);
	}
	
	public Vector2D getAcceleration() {
		return acceleration;
	}
	
	public void setAcceleration(Vector2D vec) {
		acceleration.setVector(vec);
	}
	
	public void setAcceleration(double d, double m) {
		acceleration.setDirection(d);
		acceleration.setMagnitude(m);
	}
	
	public void update() {
		velocity.addDirection(acceleration.getDirection());
		velocity.addMagnitude(acceleration.getMagnitude());
		if(velocity.getMagnitude() > maxShipMoveSpeed) velocity.setMagnitude(maxShipMoveSpeed);
		position.translate(velocity.getXY().getX(), velocity.getXY().getY());
		direction = Vector2D.wrapDirection(direction + directionAcceleration);
	}
	
	public void drawEntity(Graphics2D g2d, Rectangle bounds, EntityPlayerShip player) {
		g2d.setColor(Color.WHITE);
		g2d.drawOval((int)(this.getPosition().getX() + (bounds.width/2 - player.getPosition().getX())), (int)(this.getPosition().getY() + (bounds.height/2 - player.getPosition().getY())), 5, 5);
	}
	
	public void resetAll() {
		position.setLocation(0, 0);
		velocity.setVector(0, 0);
		acceleration.setVector(0, 0);
	}
}
